package com.yuncheng.oaApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.NkConstant;
import com.yuncheng.oaApi.entity.NkRsXmyszbb;
import com.yuncheng.oaApi.entity.NkRsYstpmx;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.mapper.NkRsYstpmxMapper;
import com.yuncheng.oaApi.service.INkRsXmyszbbService;
import com.yuncheng.oaApi.service.INkRsYstpmxService;
import com.yuncheng.oaApi.vo.Result;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 内控-预算调配明细
 * @Author: lhb
 * @Date: 2023-11-21
 * @Version: V1.0
 */
@Service
public class NkRsYstpmxServiceImpl extends BaseServiceImpl<NkRsYstpmxMapper, NkRsYstpmx> implements INkRsYstpmxService {

    @Resource
    @Lazy
    private INkRsYstpmxService nkRsYstpmxService;

    @Resource
    @Lazy
    private INkRsXmyszbbService nkRsXmyszbbService;


    /**
     *
     * @param ystpsqid 预算申请id
     * @return
     */
    public Result tpXmYsZb(String ystpsqid){

        //通过预算调配申请id 查询当前申请id下的申请明细的拨入和拨出
        //拨入
        List<NkRsYstpmx> nkRsYstpmxListBr = nkRsYstpmxService.list(new LambdaQueryWrapper<NkRsYstpmx>().eq(NkRsYstpmx::getFystpsqid, ystpsqid)
                .eq(NkRsYstpmx::getFtplx, NkConstant.YSTPMXLX_BR));
        //拨出
        List<NkRsYstpmx> nkRsYstpmxListBc = nkRsYstpmxService.list(new LambdaQueryWrapper<NkRsYstpmx>().eq(NkRsYstpmx::getFystpsqid, ystpsqid)
                .eq(NkRsYstpmx::getFtplx, NkConstant.YSTPMXLX_BC));


        if (CollectionUtil.isEmpty(nkRsYstpmxListBr)){
            throw new BusinessException("查询不到拨入预算指标");
        }

        if (CollectionUtil.isEmpty(nkRsYstpmxListBc)){
            throw new BusinessException("查询不到拨出预算指标");
        }

        //拿到 拨入 的项目预算项目id 然后查询得到数据 进行更新
        //调配明细
        NkRsYstpmx nkRsYstpmx = nkRsYstpmxListBr.get(0);
        String fxmyszbid = nkRsYstpmx.getFxmyszbid();
        //项目预算指标表
        NkRsXmyszbb nkRsXmyszbb = nkRsXmyszbbService.getById(fxmyszbid);

        BigDecimal ftpje = new BigDecimal(nkRsYstpmx.getFtpje()); //拨入金额

        BigDecimal zzb = new BigDecimal(nkRsXmyszbb.getFzzb());//项目预算指标表总指标
        BigDecimal yzfje = new BigDecimal(nkRsXmyszbb.getFyzfje()); //项目预算指标表已支付金额

        BigDecimal newzzb = zzb.add(ftpje); //新的总指标
        BigDecimal newzbjy = newzzb.subtract(yzfje); //新的总指标-已支付金额 = 新的指标结余
        BigDecimal newzcjd = yzfje.divide(newzzb, 3, BigDecimal.ROUND_HALF_UP);//新的支出进度 = 已支付/新的总指标

        nkRsXmyszbb.setFzzb(String.valueOf(newzzb));
        nkRsXmyszbb.setFzbjy(String.valueOf(newzbjy));
        nkRsXmyszbb.setFzcjd(String.valueOf(newzcjd));
        nkRsXmyszbbService.updateById(nkRsXmyszbb);

        //拿到 拨出 的项目预算项目id 然后查询得到数据 进行更新
        //调配明细
        NkRsYstpmx nkRsYstpmx1 = nkRsYstpmxListBc.get(0);
        String fxmyszbid1 = nkRsYstpmx1.getFxmyszbid();
        //项目预算指标表
        NkRsXmyszbb nkRsXmyszbb1 = nkRsXmyszbbService.getById(fxmyszbid1);

        BigDecimal tpje = new BigDecimal(nkRsYstpmx1.getFtpje());  //拨出金额

        BigDecimal fzzb = new BigDecimal(nkRsXmyszbb1.getFzzb());//项目预算指标表总指标
        BigDecimal fyzfje = new BigDecimal(nkRsXmyszbb1.getFyzfje()); //项目预算指标表已支付金额

        //新的总指标
        BigDecimal newzzb1 = fzzb.subtract(tpje);
        //新的指标结余
        BigDecimal newzbjy1 = newzzb1.subtract(fyzfje);
        //新的支出进度
        BigDecimal newzcjd1 = fyzfje.divide(newzzb1, 3, BigDecimal.ROUND_HALF_UP);
        nkRsXmyszbb1.setFzzb(String.valueOf(newzzb1));
        nkRsXmyszbb1.setFzbjy(String.valueOf(newzbjy1));
        nkRsXmyszbb1.setFzcjd(String.valueOf(newzcjd1));
        nkRsXmyszbbService.updateById(nkRsXmyszbb1);

        return Result.ok("更新成功");
    }

}
